package com.kelp.codetoolkit.ui.form;

import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.DialogWrapper
import com.kelp.codetoolkit.constant.GlobalConfig
import com.kelp.codetoolkit.dto.GenerateCodeContentDTO
import com.kelp.codetoolkit.service.CreateBusinessLogicFileService
import java.awt.FlowLayout
import javax.swing.*;

/**
 * @author: liuyuebai
 * @date: 2024/7/31 13:20
 * @description:
 */
class DatabaseToCodeFormDialog(project: Project):DialogWrapper(project)  {

    private var project: Project? = null

    /**
     * 主面板
     */
    private val mainPanel: JPanel = JPanel()

    /**
     * 项目模块选择
     */
    private val modulePanel: JPanel = JPanel(FlowLayout(FlowLayout.LEFT))

    /**
     *
     */
    private val moduleLabel: JLabel = JLabel("Code Module：")

    /**
     * 项目模块下拉框
     */
    private var moduleComboBox: ComboBox<String> = ComboBox()

    private val packageNamePanel: JPanel = JPanel(FlowLayout(FlowLayout.LEFT))

    /**
     *
     */
    private val packageNameLabel: JLabel = JLabel("FIle Package Name：")

    /**
     * 包名
     */
    private var parentPackageName: JTextField = JTextField(15)


    /**
     * 选择生成代码
     */
    private var templatePanel: JPanel = JPanel()


    init {
        title = GlobalConfig.getCodeToolKitPanelTitle()
        this.project = project
        initModuleComboBox(project)
        init()
    }

    override fun createCenterPanel(): JComponent{
        mainPanel.layout = BoxLayout(mainPanel, BoxLayout.Y_AXIS) // 设置mainPanel的布局为垂直排列
        modulePanel.add(moduleLabel)
        modulePanel.add(moduleComboBox)
        mainPanel.add(modulePanel)
        packageNamePanel.add(packageNameLabel)
        packageNamePanel.add(parentPackageName)
        mainPanel.add(packageNamePanel)
        return mainPanel
    }

    override fun doOKAction() {
        super.doOKAction()
        val codeParams = GenerateCodeContentDTO()
        codeParams.setModuleName(moduleComboBox.selectedItem as String)
        codeParams.setParentPackageName(parentPackageName.text)
        project?.let { CreateBusinessLogicFileService.getInstance(it).databaseToBusinessLoginServiceFIle(it,codeParams) }
    }

    private fun initModuleComboBox(project: Project){
        val moduleList = ModuleManager.getInstance(project).modules
        for (module in moduleList){
            moduleComboBox.addItem(module.name)
        }
    }
}
